﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Contents
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class AdvDAO
    {

        #region general
        private static Adv LoadObj(IDataReader datareader)
        {
            Adv obj = new Adv();
            obj.AdvID = MpDataHelp.GetInt(datareader, "AdvID");
            obj.Height = MpDataHelp.GetInt(datareader, "Height");
            obj.Width = MpDataHelp.GetInt(datareader, "Width");
            obj.Region = MpDataHelp.GetInt(datareader, "Region");
            obj.Target = MpDataHelp.GetString(datareader, "Target");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Image = MpDataHelp.GetString(datareader, "Image");
            obj.Link = MpDataHelp.GetString(datareader, "Link");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.IsFlash = MpDataHelp.GetBoolean(datareader, "IsFlash");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");

            return obj;
        }
        #endregion

        public static int Adv_Add(Adv obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Adv_Add");
            db.AddOutParameter(dbCommand, "AdvID", DbType.Int32, obj.AdvID);
            db.AddInParameter(dbCommand, "Height", DbType.Int32, obj.Height);
            db.AddInParameter(dbCommand, "Width", DbType.Int32, obj.Width);
            db.AddInParameter(dbCommand, "Region", DbType.Int32, obj.Region);
            db.AddInParameter(dbCommand, "Target", DbType.String, obj.Target);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "Link", DbType.String, obj.Link);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsFlash", DbType.Boolean, obj.IsFlash);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@AdvID"));
            return -1;
        }
        public static int Adv_Update(Adv obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Adv_Update");
            db.AddInParameter(dbCommand, "AdvID", DbType.Int32, obj.AdvID);
            db.AddInParameter(dbCommand, "Height", DbType.Int32, obj.Height);
            db.AddInParameter(dbCommand, "Width", DbType.Int32, obj.Width);
            db.AddInParameter(dbCommand, "Region", DbType.Int32, obj.Region);
            db.AddInParameter(dbCommand, "Target", DbType.String, obj.Target);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "Link", DbType.String, obj.Link);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsFlash", DbType.Boolean, obj.IsFlash);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Adv_Delete(Int32 AdvID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Adv_Delete");
            db.AddInParameter(dbCommand, "AdvID", DbType.Int32, AdvID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Adv> Adv_List(int AdvID, int Region, int IsActive)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Adv_List");
            db.AddInParameter(dbCommand, "AdvID", DbType.Int32, AdvID);
            db.AddInParameter(dbCommand, "Region", DbType.Int32, Region);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            List<Adv> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Adv>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



